﻿using System.Data;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.AnalysisServices.AdomdClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Symantec.DataFactories;
using System;

namespace DataFactoriesTests
{


    /// <summary>
    ///This is a test class for IDataFactoryTest and is intended
    ///to contain all IDataFactoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IDataFactoryTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private struct DataFactories
        {
            public const string SqlClientDataFactory = "Symantec.DataFactories.SqlClientDataFactory";
            public const string SqlConnectionString = "Data Source=TUS1SYMDBSDIN01\\SYMDBDEV,1433;Network=DBMSSOCN;Initial Catalog=WebSpt;uid=bar_webuser;password=chansle_114234;Persist Security Info=True;";

            public const string AdomdClientDataFactory = "Symantec.DataFactories.AdomdClientDataFactory";
            public const string AdomdConnectionString = "Data Source=TUS1SYMDBSDIN01\\SYMASDEV_RPT;Initial Catalog=Symmetrics;";
        }


        internal virtual IDataFactory CreateIDataFactory(string dataFactory)
        {
            IDataFactory target = null;
            switch (dataFactory)
            {
                case DataFactories.SqlClientDataFactory:
                    target = SqlClientDataFactory.Instance;
                    break;
                case DataFactories.AdomdClientDataFactory:
                    target = AdomdClientDataFactory.Instance;
                    break;
                default:
                    break;
            }
            return target;
        }

        /// <summary>
        ///A test for CreateConnection
        ///</summary>
        [TestMethod()]
        public void CreateConnectionTest()
        {
            IDataFactory target;
            IDbConnection expected;
            IDbConnection actual;

            // Test that the Symantec.DataFactories.SqlClientDataFactory creates a SqlConnection 
            // object which complies with the IDbConnection interface.
            target = CreateIDataFactory(DataFactories.SqlClientDataFactory);
            expected = new SqlConnection();
            actual = target.CreateConnection();
            Assert.IsInstanceOfType(actual, expected.GetType());

            // Test that the Symantec.DataFactories.AdomdClientDataFactory creates a AdomdConnection 
            // object which complies with the IDbConnection interface.
            target = CreateIDataFactory(DataFactories.AdomdClientDataFactory);
            expected = new AdomdConnection();
            actual = target.CreateConnection();
            Assert.IsInstanceOfType(actual, expected.GetType());
        }

        /// <summary>
        ///A test for CreateCommand
        ///</summary>
        [TestMethod()]
        public void CreateCommandTest()
        {
            IDataFactory target;
            IDbConnection connection;
            IDbCommand expected;
            IDbCommand actual;

            // Test that the Symantec.DataFactories.SqlClientDataFactory creates a SqlConnection 
            // object which complies with the IDbConnection interface.
            target = CreateIDataFactory(DataFactories.SqlClientDataFactory);
            connection = target.CreateConnection();
            expected = new SqlCommand();
            actual = target.CreateCommand();
            Assert.IsInstanceOfType(actual, expected.GetType());

            // Test that the Symantec.DataFactories.AdomdClientDataFactory creates a AdomdConnection 
            // object which complies with the IDbConnection interface.
            target = CreateIDataFactory(DataFactories.AdomdClientDataFactory);
            connection = target.CreateConnection();
            expected = new AdomdCommand();
            actual = target.CreateCommand();
            Assert.IsInstanceOfType(actual, expected.GetType());
        }

        /// <summary>
        ///A test for CreateDataAdapter
        ///</summary>
        [TestMethod()]
        public void CreateDataAdapterTest()
        {
            IDataFactory target;
            IDataAdapter expected;
            IDataAdapter actual;

            // Test that the Symantec.DataFactories.SqlClientDataFactory creates a SqlDataAdapter 
            // object which complies with the IDbDataAdapter interface.
            target = CreateIDataFactory(DataFactories.SqlClientDataFactory);
            expected = new SqlDataAdapter();
            actual = target.CreateDataAdapter();
            Assert.IsInstanceOfType(actual, expected.GetType());

            // Test that the Symantec.DataFactories.AdomdClientDataFactory creates a AdomdDataAdapter 
            // object which complies with the IDbDataAdapter interface.
            target = CreateIDataFactory(DataFactories.AdomdClientDataFactory);
            expected = new AdomdDataAdapter();
            actual = target.CreateDataAdapter();
            Assert.IsInstanceOfType(actual, expected.GetType());
        }

        /// <summary>
        ///A test for ExecuteXmlReader
        ///</summary>
        [TestMethod()]
        public void ExecuteXmlReaderTest()
        {
            IDataFactory target;
            IDbConnection connection;
            XmlReader actual;

            // Test that the Symantec.DataFactories.SqlClientDataFactory returns an XmlReader object 
            // via the IDataFactory.ExecuteXmlReader() method.
            target = CreateIDataFactory(DataFactories.SqlClientDataFactory);
            connection = target.CreateConnection();
            connection.ConnectionString = DataFactories.SqlConnectionString;

            using (connection)
            {
                IDbCommand command = target.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = "select * from SymUsers for xml auto";

                connection.Open();
                actual = target.ExecuteXmlReader();
            }
            Assert.IsInstanceOfType(actual, typeof(XmlReader));

            // Test that the Symantec.DataFactories.AdomdClientDataFactory returns an XmlReader object 
            // via the IDataFactory.ExecuteXmlReader() method.
            target = CreateIDataFactory(DataFactories.AdomdClientDataFactory);
            connection = target.CreateConnection();
            connection.ConnectionString = DataFactories.AdomdConnectionString;

            using (connection)
            {
                IDbCommand command = target.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = @"
                    SELECT
                    {
                        [Measures].[Count Backlog]
                    } ON COLUMNS,
                    {
                        LastPeriods(4, [Date Backlogged].[F Week].&[2.01047E3])
                    } ON ROWS
                    FROM
                        [Backlog]";

                connection.Open();
                actual = target.ExecuteXmlReader();
            }
            Assert.IsInstanceOfType(actual, typeof(XmlReader));
        }

        /// <summary>
        ///A test for SupportsXmlReader
        ///</summary>
        [TestMethod()]
        public void SupportsXmlReaderTest()
        {
            IDataFactory target;
            bool actual;

            // Test that the Symantec.DataFactories.SqlClientDataFactory class supports XML Reader
            target = CreateIDataFactory(DataFactories.SqlClientDataFactory);
            actual = target.SupportsXmlReader;
            Assert.IsTrue(actual);

            // Test that the Symantec.DataFactories.AdomdClientDataFactory class supports XML Reader
            target = CreateIDataFactory(DataFactories.AdomdClientDataFactory);
            actual = target.SupportsXmlReader;
            Assert.IsTrue(actual);
        }

        /// <summary>
        ///A test for ExecuteReader
        ///</summary>
        [TestMethod()]
        public void ExecuteReaderTest()
        {
            IDataFactory target;
            IDbConnection connection;
            IDataReader actual;

            // Test that the Symantec.DataFactories.SqlClientDataFactory returns an IDataReader object 
            // via the IDataFactory.ExecuteReader() method.
            target = CreateIDataFactory(DataFactories.SqlClientDataFactory);
            connection = target.CreateConnection();
            connection.ConnectionString = DataFactories.SqlConnectionString;

            using (connection)
            {
                IDbCommand command = target.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = "select * from SymUsers";

                connection.Open();
                actual = target.ExecuteReader();
            }
            Assert.IsInstanceOfType(actual, typeof(IDataReader));

            // Test that the Symantec.DataFactories.AdomdClientDataFactory returns an IDataReader object 
            // via the IDataFactory.ExecuteReader() method.
            target = CreateIDataFactory(DataFactories.AdomdClientDataFactory);
            connection = target.CreateConnection();
            connection.ConnectionString = DataFactories.AdomdConnectionString;

            using (connection)
            {
                IDbCommand command = target.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = @"
                    SELECT
                    {
                        [Measures].[Count Backlog]
                    } ON COLUMNS,
                    {
                        LastPeriods(4, [Date Backlogged].[F Week].&[2.01047E3])
                    } ON ROWS
                    FROM
                        [Backlog]";

                connection.Open();
                actual = target.ExecuteReader();
            }
            Assert.IsInstanceOfType(actual, typeof(IDataReader));
        }

        /// <summary>
        ///A test for Fill
        ///</summary>
        [TestMethod()]
        public void FillTest()
        {
            IDataFactory dataFactory;
            DataSet dsActual;
            int iTarget, iActual;
            IDbConnection connection;

            // Test that the Symantec.DataFactories.SqlClientDataFactory fills a DataTable and returns
            // the number of rows added to that DataTable.
            dataFactory = CreateIDataFactory(DataFactories.SqlClientDataFactory);
            connection = dataFactory.CreateConnection();
            connection.ConnectionString = DataFactories.SqlConnectionString;
            dsActual = new DataSet();
            iActual = 0;

            using (connection)
            {
                IDbCommand command = dataFactory.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = "select * from SymUsers";

                IDataAdapter dataAdapter = dataFactory.CreateDataAdapter();
                iActual = dataFactory.Fill(dsActual);
            }

            iTarget = dsActual.Tables[0].Rows.Count;
            Assert.IsTrue(iActual > 0);
            Assert.AreEqual(iTarget, iActual);

            // Test that the Symantec.DataFactories.AdomdClientDataFactory fills a DataTable and returns
            // the number of rows added to that DataTable.
            dataFactory = CreateIDataFactory(DataFactories.AdomdClientDataFactory);
            connection = dataFactory.CreateConnection();
            connection.ConnectionString = DataFactories.AdomdConnectionString;
            dsActual = new DataSet();
            iActual = 0;

            using (connection)
            {
                IDbCommand command = dataFactory.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = @"
                    SELECT
                    {
                        [Measures].[Count Backlog]
                    } ON COLUMNS,
                    {
                        LastPeriods(4, [Date Backlogged].[F Week].&[2.01047E3])
                    } ON ROWS
                    FROM
                        [Backlog]";

                IDataAdapter dataAdapter = dataFactory.CreateDataAdapter();
                iActual = dataFactory.Fill(dsActual);
            }

            iTarget = dsActual.Tables[0].Rows.Count;
            Assert.IsTrue(iActual > 0);
            Assert.AreEqual(iTarget, iActual);
        }
    }
}
